package cn.com.open.itembank.logic.questiontype;

import cn.com.open.itembank.common.Function;
import cn.com.open.itembank.common.enums.ItemType;
import cn.com.open.itembank.dto.QuestionImportModel;
import cn.com.open.itembank.dto.QuestionModel;
import org.springframework.util.StringUtils;

/**
 * Created by lucky on 2017/12/26.
 */
public class BaseSubItem extends QuestionTypeBase {

    /**
     * 是否拥有子试题题干.
     */
    protected boolean hasSubPrompt = true;

    /**
     * Instantiates a new Base sub item.
     *
     * @param model the model
     */
    public BaseSubItem(QuestionImportModel model) {
        super(model);
    }

    //region 校验是否为空

    /**
     * 校验是否为空.
     *
     * @return the boolean
     */
    @Override
    protected boolean checkEmptySelf() {
        boolean result = true;

        if (importModel.getSubItems().isEmpty()) {
            importModel.setErrorInfo("缺少子试题；");
            return false;
        }

        for (int i = 0; i < importModel.getSubItems().size(); i++) {
            QuestionImportModel item = importModel.getSubItems().get(i);

            if (hasSubPrompt && StringUtils.isEmpty(item.getPrompt())) {
                importModel.setErrorInfo(String.format("第 %d 道子题题干不能为空；", i + 1));
                result = false;
            }

            if (item.getChoices().isEmpty() && !item.getQuestionTypeName().equals("问答题") && !item.getQuestionTypeName().equals("填空题")) {
                importModel.setErrorInfo(String.format("第 %d 道子题选项不能为空；", i + 1));
                result = false;
            }

            if (item.getAnswer().isEmpty()) {
                importModel.setErrorInfo(String.format("第 %d 道子题答案不能为空；", i + 1));
                result = false;
            }
        }

        return result;
    }
    //endregion

    //region 校验答案格式

    /**
     * 校验答案格式.
     *
     * @return the boolean
     */
    @Override
    protected boolean checkAnswerFormat() {
        boolean result = true;

        for (int i = 0; i < importModel.getSubItems().size(); i++) {
            QuestionImportModel item = importModel.getSubItems().get(i);

            for (String answer : item.getAnswer()) {
                if (answerIdList.indexOf(answer.trim().toUpperCase()) < 0 && !item.getQuestionTypeName().equals("填空题") && !item.getQuestionTypeName().equals("问答题")) {
                    importModel.setErrorInfo(String.format("第 %d 道子题答案格式不正确；", i + 1));
                    return false;
                }
            }
        }
        return result;
    }
    //endregion

    //region 校验答案界限

    /**
     * 校验答案界限.
     *
     * @return the boolean
     */
    @Override
    protected boolean checkAnswerLimit() {
        boolean result = true;

        for (int i = 0; i < importModel.getSubItems().size(); i++) {
            QuestionImportModel item = importModel.getSubItems().get(i);

            for (String answer : item.getAnswer()) {
                if (answerIdList.indexOf(answer.trim().toUpperCase()) > item.getChoices().size() - 1 && !item.getQuestionTypeName().equals("问答题") && !item.getQuestionTypeName().equals("填空题") && !item.getQuestionTypeName().equals("判断题")) {
                    importModel.setErrorInfo(String.format("第 %d 道子题答案英文字符超出界限；", i + 1));
                    return false;
                }
            }
        }

        return result;
    }
    //endregion

    //region 获取保存模型

    /**
     * 获取保存模型.
     *
     * @return the input question model
     */
    @Override
    public QuestionModel getSaveModel() {
        QuestionModel model = super.getSaveModel();
        for (QuestionImportModel sub : importModel.getSubItems()) {
            QuestionModel subModel = new QuestionModel();

            subModel.setI2(sub.getPrompt());
            if(sub.getQuestionTypeName().equals("单选题")){
                subModel.setI3(ItemType.SINGLECHOICE.getIndex());
            }
            if(sub.getQuestionTypeName().equals("多选题")){
                subModel.setI3(ItemType.MULTIPLECHOICE.getIndex());
            }
            if(sub.getQuestionTypeName().equals("判断题")){
                subModel.setI3(ItemType.JUDGEMENT.getIndex());
            }
            if(sub.getQuestionTypeName().equals("填空题")){
                subModel.setI3(ItemType.TEXTENTRY.getIndex());
            }
            if(sub.getQuestionTypeName().equals("问答题")){
                subModel.setI3(ItemType.EXTENDEDTEXT.getIndex());
            }
//            if(sub.getQuestionTypeName().equals("匹配题")){
//                subModel.setI3(ItemType.MATCH.getIndex());
//            }
//            if(sub.getQuestionTypeName().equals("阅读理解")){
//                subModel.setI3(ItemType.READINGCOMPREHENSION.getIndex());
//            }
//            if(sub.getQuestionTypeName().equals("完形填空")){
//                subModel.setI3(ItemType.CLOZE.getIndex());
//            }
//            if(sub.getQuestionTypeName().equals("排序题")){
//                subModel.setI3(ItemType.ORDER.getIndex());
//            }
//            if(sub.getQuestionTypeName().equals("上传题")){
//                subModel.setI3(ItemType.UPLOAD.getIndex());
//            }

            subModel.setI10(sub.getQuestionAnalysis());

            if(!sub.getQuestionTypeName().equals("问答题") && !sub.getQuestionTypeName().equals("填空题")) {
                //I6,I7 需要各种题型重载完成
                for (String choice : sub.getChoices()) {
                    subModel.getI6().add(choice);
                }
                //answer存的是abcd，i7需要0123
                for (String answer : sub.getAnswer()) {
                    subModel.getI7().add(String.valueOf(Function.getUpperCaseIndex(answer)));
                }
            }else{
                for (String answer : sub.getAnswer()) {
                    subModel.getI6().add(answer);
                }
            }

            model.getSub().add(subModel);
        }

        //I17 子试题个数 需要各种题型重载完成
        model.setI17(importModel.getSubItems().size());

        return model;
    }
    //endregion
}
